package com.springcloud.market.user.service;

import com.springcloud.market.user.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;

@Slf4j
@Service
public class UserRegisterMessageSender {
    private final StreamBridge streamBridge;

    public UserRegisterMessageSender(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    }

    public void sendUserRegisterMessageSender(String userId, String phoneNumber, String username){
        // 构造消息内容
        UserDTO userDTO = new UserDTO(userId, username,phoneNumber, Instant.now().toString());

// 使用 StreamBridge 发送消息
        boolean sent = streamBridge.send(
                "userRegisterConsumer-out-0",
                MessageBuilder.withPayload(userDTO).build());

        if (sent) {
            log.info("用户注册成功消息已发送，用户ID：{}", userId);
        } else {
            log.error("用户注册成功消息发送失败，用户ID：{}", userId);
        }
    }
}
